//
//  FQHelpCenterDetailViewController.swift
//  SwiftProject
//
//  Created by 董駸 on 2022/9/5.
//

import UIKit

class FQHelpCenterDetailViewController: BaseViewController, ListAdapterDataSource {
    lazy var adapter : ListAdapter = {
        ListAdapter(updater: ListAdapterUpdater(), viewController: self)
    }()
    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    var data : [BaseIGListKitModel] = []
    
    var baseModel : FQHelpCenterModel!
    var indexPath : IndexPath!
    
    convenience init(indexPath : IndexPath, baseModel : FQHelpCenterModel) {
        self.init(nibName: nil, bundle: nil)
        self.indexPath = indexPath
        self.baseModel = baseModel
        
        let model1 = FQAnswerModel()
        model1.model = baseModel.QAndA[indexPath.row]
        
        let model2 = FQOtherQuestionModel()
        model2.list = baseModel.QAndA.filter({ model in
            indexPath.row != baseModel.QAndA.firstIndex(of: model)
        })
        self.data = [model1, model2, FQHelpTipModel()]
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBarType = .light
    }
    
    override func initSubviews() {
        super.initSubviews()
        
        collectionView.contentInsetAdjustmentBehavior = .never
        collectionView.backgroundColor = .qmui_color(withHexString: "ecf2fc")
        collectionView.alwaysBounceVertical = true
        view.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { make in
            make.left.right.equalTo(view)
            make.top.equalTo(NavigationContentTop)
            make.bottom.equalTo(-TabBarHeight)
        }
        
        adapter.collectionView = collectionView
        adapter.dataSource = self
    }
    
    func objects(for listAdapter: ListAdapter) -> [ListDiffable] {
        data
    }
    
    func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any) -> ListSectionController {
        switch object {
        case is FQAnswerModel:
            return FQAnswerSectionController()
        case is FQHelpTipModel:
            return FQHelpTipController()
        default:
            return FQOtherQuestionController()
        }
    }
    
    func emptyView(for listAdapter: ListAdapter) -> UIView? {
        nil
    }
    
    func reload(question : String) {
        let model1 = FQAnswerModel()
        model1.model = baseModel.QAndA.filter({ model in
            model.question == question
        }).first!
        
        let model2 = FQOtherQuestionModel()
        model2.list = baseModel.QAndA.filter({ model in
            model1.model != model
        })
        data = [model1, model2, FQHelpTipModel()]
        adapter.reloadData(completion: nil)
    }
    
    override func setupNavigationItems() {
        super.setupNavigationItems()
        title = "问题详情"
    }
}
